package com.itestor.iptv.modules.ne.service.impl;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.hibernate.Session;

import com.itestor.iptv.modules.common.bean.RobotRealtimeQuery;
import com.itestor.iptv.modules.common.dao.TbRegionManageDao;
import com.itestor.iptv.modules.common.domain.GroupManage;
import com.itestor.iptv.modules.common.domain.QosRecord;
import com.itestor.iptv.modules.common.domain.RegionManage;
import com.itestor.iptv.modules.common.domain.RoleGroup;
import com.itestor.iptv.modules.common.domain.RoleGroupRobot;
import com.itestor.iptv.modules.common.vo.RegionManageBean;
import com.itestor.iptv.modules.common.vo.RegionManageOBJ;
import com.itestor.iptv.modules.common.vo.RobotRealtimeBaan;
import com.itestor.iptv.modules.common.vo.User;
import com.itestor.iptv.modules.ne.bean.RobotChooseQuery;
import com.itestor.iptv.modules.ne.bean.RobotManageQuery;
import com.itestor.iptv.modules.ne.bean.RobotManageTvWallQuery;
import com.itestor.iptv.modules.ne.bean.RobotSelectQuery;
import com.itestor.iptv.modules.ne.bean.RoleSelRobotQuery;
import com.itestor.iptv.modules.ne.dao.RobotCfgDao;
import com.itestor.iptv.modules.ne.dao.RobotFileInfoDao;
import com.itestor.iptv.modules.ne.dao.RobotManageDao;
import com.itestor.iptv.modules.ne.dao.RobotNetInfoDao;
import com.itestor.iptv.modules.ne.domain.IptvLostRate;
import com.itestor.iptv.modules.ne.domain.IptvQuality;
import com.itestor.iptv.modules.ne.domain.RobotCFG;
import com.itestor.iptv.modules.ne.domain.RobotFileInfo;
import com.itestor.iptv.modules.ne.domain.RobotManage;
import com.itestor.iptv.modules.ne.domain.RobotNetInfo;
import com.itestor.iptv.modules.ne.service.RobotManageService;
import com.itestor.iptv.modules.ne.vo.RobotCFGOBJ;
import com.itestor.iptv.modules.ne.vo.RobotFileInfoOBJ;
import com.itestor.iptv.modules.ne.vo.RobotManageBaan;
import com.itestor.iptv.modules.ne.vo.RobotManageOBJ;
import com.itestor.iptv.modules.ne.vo.RobotNetInfoOBJ;
import com.itestor.iptv.modules.ne.vo.RobotNodeStatusBean;
import com.itestor.iptv.modules.ne.vo.RobotSelectBaan;
import com.itestor.iptv.modules.ne.vo.RoleRobotSelBaan;
import com.itestor.iptv.utils.BeanConverter;
import com.itestor.iptv.utils.JdbcUtil;

/**
 * 
 * @author Mr.li
 * 
 */
public class RobotManageServiceImpl implements RobotManageService {

    private static final Logger logger = Logger
            .getLogger(RobotManageServiceImpl.class);

    private RobotManageDao robotManageDao;

    private RobotCfgDao robotCfgDao;

    private RobotFileInfoDao robotFileInfoDao;

    private RobotNetInfoDao robotNetInfoDao;

    private TbRegionManageDao tbRegionManageDao;

    private final DateFormat DATE_FAMAT = new SimpleDateFormat("yyyy-MM-dd",
            Locale.UK);

    public RobotManageOBJ get(String name) {
        User user = null;
        StringBuilder hql = new StringBuilder();
        hql.append("from RobotManage t ");
        if (!StringUtils.isEmpty(name)) {
            hql.append(" where t.name = '" + name + "'");
        }
        Query query = robotManageDao.getHibernateSession().createQuery(
                hql.toString());
        RobotManage robotManage = (RobotManage) query.uniqueResult();
        RobotManageOBJ robotManageOBJ = new RobotManageOBJ();
        if (robotManage != null) {
            robotManageOBJ = convertToRobotManageOBJ(robotManage);
        }
        return robotManageOBJ;
    }

    public RobotManageOBJ get(int id) {
        RobotManageOBJ robotManageOBJ = null;
        RobotManage robotManage = robotManageDao.find(id);
        if (robotManage != null) {
            robotManageOBJ = convertToRobotManageOBJ(robotManage);
            RegionManage regionManage = robotManage.getRegionManage();
            RegionManageOBJ regionManageOBJ = new RegionManageOBJ();
            if (null != regionManage) {
                regionManageOBJ.setIdRegion(regionManage.getIdRegion());
                regionManageOBJ.setPrentRegionId(regionManageOBJ
                        .getPrentRegionId());
                regionManageOBJ.setRegionName(regionManage.getRegionName());
            }
            robotManageOBJ.setRegionManageOBJ(regionManageOBJ);
        }
        return robotManageOBJ;
    }

    /**
     * @param robotManageQuery
     */
    @SuppressWarnings("unchecked")
    public void queryRobotManageList(RobotManageQuery robotManageQuery) {
        List<RobotManageBaan> resultList = new ArrayList<RobotManageBaan>();
        StringBuilder hql = new StringBuilder();
        hql
                .append("select {t.*},{n.*} from ROBOTMANAGE t left join ROBOTNETINFO n on t.IDROBOT=n.IDROBOT  WHERE 1=1 ");

        if (null != robotManageQuery.getName()) {
            hql.append(" and t.NAME like:name");
        }

        Query query = robotManageDao.getHibernateSession().createSQLQuery(
                hql.toString()).addEntity("t", RobotManage.class).addEntity(
                "n", RobotNetInfo.class);

        if (null != robotManageQuery.getName()) {
            query.setParameter("name", "%" + robotManageQuery.getName() + "%");
        }

        if (robotManageQuery.getPagerCount() == -1) {
            robotManageQuery.setPagerCount(query.list().size());
        }

        query.setFirstResult(robotManageQuery.getPagerOffset());
        query.setMaxResults(robotManageQuery.getPagerPerPage());

        List<Object[]> list = query.list();

        for (Object[] obj : list) {
            RobotManageBaan bean = new RobotManageBaan();
            if (null != (RobotManage) obj[0]) {
                bean.setRobotManage((RobotManage) obj[0]);
            } else {
                bean.setRobotManage(new RobotManage());
            }

            if (null != (RobotNetInfo) obj[1]) {
                bean.setRobotNetInfo((RobotNetInfo) obj[1]);
            } else {
                bean.setRobotNetInfo(new RobotNetInfo());
            }

            resultList.add(bean);
        }

        robotManageQuery.setQueryResults(resultList);
    }

    /**
     * @param robotChooseQuery
     */
    @SuppressWarnings("unchecked")
    public void queryRobotChooseList(RobotChooseQuery robotChooseQuery) {

        List<RobotSelectBaan> resultList = new ArrayList<RobotSelectBaan>();
        StringBuilder hql = new StringBuilder();
        hql
                .append("select {t.*},{n.*} from ROBOTMANAGE t left join ROBOTNETINFO n on t.IDROBOT=n.IDROBOT  WHERE 1=1 ");

        if (null != robotChooseQuery.getIdRegionlist()) {
            hql.append(" and t.IDREGION in (:idList)");
        }

        if (null != robotChooseQuery.getRobotName()) {
            hql.append(" and t.NAME like:name");
        }

        Query query = robotManageDao.getHibernateSession().createSQLQuery(
                hql.toString()).addEntity("t", RobotManage.class).addEntity(
                "n", RobotNetInfo.class);

        if (null != robotChooseQuery.getIdRegionlist()) {
            query
                    .setParameterList("idList", robotChooseQuery
                            .getIdRegionlist());
        }

        if (null != robotChooseQuery.getRobotName()) {
            query.setParameter("name", "%" + robotChooseQuery.getRobotName()
                    + "%");
        }

        if (robotChooseQuery.getPagerCount() == -1) {
            robotChooseQuery.setPagerCount(query.list().size());
        }

        query.setFirstResult(robotChooseQuery.getPagerOffset());
        query.setMaxResults(robotChooseQuery.getPagerPerPage());

        List<Object[]> list = query.list();

        for (Object[] obj : list) {
            RobotSelectBaan bean = new RobotSelectBaan();
            if (null != (RobotManage) obj[0]) {
                bean.setRobotManage((RobotManage) obj[0]);
            } else {
                bean.setRobotManage(new RobotManage());
            }

            if (null != (RobotNetInfo) obj[1]) {
                bean.setRobotNetInfo((RobotNetInfo) obj[1]);
            } else {
                bean.setRobotNetInfo(new RobotNetInfo());
            }

            resultList.add(bean);
        }

        robotChooseQuery.setQueryResults(resultList);

    }

    /**
     * @param robotSelectQuery
     */
    @SuppressWarnings("unchecked")
    public void queryRobotSelectList(RobotSelectQuery robotSelectQuery) {
        List<RobotSelectBaan> resultList = new ArrayList<RobotSelectBaan>();
        StringBuilder hql = new StringBuilder();
        hql
                .append("select {t.*},{n.*},{m.*} from ROBOTMANAGE t left join ROBOTNETINFO n on t.IDROBOT=n.IDROBOT ,GROUPMANAGE m ,ROBOTGROUP g WHERE "
                        + " m.idgroup=g.idgroup and g.idRobot =t.idrobot ");

        if (null != robotSelectQuery.getQueryIdGroup()) {
            hql.append(" AND g.idgroup =:idGroup");
        }
        Query query = robotManageDao.getHibernateSession().createSQLQuery(
                hql.toString()).addEntity("t", RobotManage.class).addEntity(
                "n", RobotNetInfo.class).addEntity("m", GroupManage.class);

        if (null != robotSelectQuery.getQueryIdGroup()) {
            query.setParameter("idGroup", robotSelectQuery.getQueryIdGroup());
        }
        if (robotSelectQuery.getPagerCount() == -1) {
            robotSelectQuery.setPagerCount(query.list().size());
        }
        query.setFirstResult(robotSelectQuery.getPagerOffset());
        query.setMaxResults(robotSelectQuery.getPagerPerPage());

        List<Object[]> list = query.list();
        for (Object[] obj : list) {
            RobotSelectBaan bean = new RobotSelectBaan();
            if (null != (RobotManage) obj[0]) {
                bean.setRobotManage((RobotManage) obj[0]);
            } else {
                bean.setRobotManage(new RobotManage());
            }
            if (null != (RobotNetInfo) obj[1]) {
                bean.setRobotNetInfo((RobotNetInfo) obj[1]);
            } else {
                bean.setRobotNetInfo(new RobotNetInfo());
            }
            if (null != (GroupManage) obj[2]) {
                bean.setGroupManage((GroupManage) obj[2]);
            } else {
                bean.setGroupManage(new GroupManage());
            }
            resultList.add(bean);
        }
        robotSelectQuery.setQueryResults(resultList);
    }

    /**
     * 查询实时数据
     * @param pRobotRealtimeQuery
     */
    @SuppressWarnings("unchecked")
    public void queryRealtimeDataList(RobotRealtimeQuery pRobotRealtimeQuery) {
        List<RobotRealtimeBaan> resultList = new ArrayList<RobotRealtimeBaan>();
        StringBuilder hql = new StringBuilder();
        hql.append("select {t.*} from ROBOTMANAGE t  WHERE 1=1 ");

        if (null != pRobotRealtimeQuery.getRobotName()) {
            hql.append(" and t.NAME like :name ");
        }

        Query query = robotManageDao.getHibernateSession().createSQLQuery(
                hql.toString()).addEntity("t", RobotManage.class);

        if (null != pRobotRealtimeQuery.getRobotName()) {
            query.setParameter("name", "%" + pRobotRealtimeQuery.getRobotName()
                    + "%");
        }

        if (pRobotRealtimeQuery.getPagerCount() == -1) {
            pRobotRealtimeQuery.setPagerCount(query.list().size());
        }

        query.setFirstResult(pRobotRealtimeQuery.getPagerOffset());
        query.setMaxResults(pRobotRealtimeQuery.getPagerPerPage());

        List<RobotManage> list = query.list();
        for (RobotManage obj : list) {
            RobotRealtimeBaan bean = new RobotRealtimeBaan();
            RobotManage manage = (RobotManage) obj;
            bean.setRobotManage(manage);
            resultList.add(bean);
        }

        pRobotRealtimeQuery.setQueryResults(resultList);
    }

    @SuppressWarnings("unchecked")
    public void queryRobotManageRegionList(
            RobotManageTvWallQuery robotManageQuery) {
        // StringBuilder hql = new StringBuilder();
        // hql.append("select t.IDROBOT,t.NAME,r.REGIONNAME,t.CHANNEL from
        // ROBOTMANAGE t left join (select rr.IDROBOT IDROBOT,rm.REGIONNAME
        // REGIONNAME from REGIONMANAGE rm,ROBOTREGION rr where rr.idRegion =
        // rm.idRegion) r on r.IDROBOT=t.IDROBOT where (1=1) ");
        // if (!StringUtils.isEmpty(robotManageQuery.getName())) {
        // hql.append(" and t.NAME like '%" + robotManageQuery.getName()
        // + "%'");
        // }
        // if (!StringUtils.isEmpty(robotManageQuery.getRegion())) {
        // hql.append("and t.IDROBOT in(select rr.IDROBOT from REGIONMANAGE
        // rm,ROBOTREGION rr where rr.idRegion = rm.idRegion and rm.REGIONNAME
        // like '%" + robotManageQuery.getRegion() + "%')");
        // }
        // if (!StringUtils.isEmpty(robotManageQuery.getId())) {
        // hql.append("and t.IDROBOT in(select rr.IDROBOT from ROBOTREGION rr
        // where rr.idRegion in( " + robotManageQuery.getId() + "))");
        // }
        // Query query = robotManageDao.getHibernateSession().createSQLQuery(
        // hql.toString());
        // if (robotManageQuery.getPagerCount() == -1) {
        // robotManageQuery.setPagerCount(query.list().size());
        // }
        // query.setFirstResult(robotManageQuery.getPagerOffset());
        // query.setMaxResults(robotManageQuery.getPagerPerPage());
        //
        // robotManageQuery.setQueryResults(query.list());

        StringBuilder hql = new StringBuilder();
        hql
                .append("select t.IDROBOT,t.NAME,r.REGIONNAME,t.CHANNEL from ROBOTMANAGE t ,REGIONMANAGE  r where t.IDREGION = r.IDREGION ");

        if (!StringUtils.isEmpty(robotManageQuery.getName())) {
            hql.append(" and t.NAME like '%" + robotManageQuery.getName()
                    + "%'");
        }

        Set<Integer> idList = new HashSet<Integer>();
        if (!StringUtils.isEmpty(robotManageQuery.getId())) {

            hql.append(" and t.IDREGION in (" + robotManageQuery.getId() + ")");

            // if (null != robotManageQuery.getId()) {
            // List<RegionManageBean> childList = tbRegionManageDao
            // .getChildRegionList(Integer.parseInt(robotManageQuery
            // .getId()));
            // for (RegionManageBean bean : childList) {
            // idList.add(bean.getIdRegion());
            // }
            // //
            // idList.add(Integer.parseInt(robotManageQuery.getId()));
            // }

        }

        Query query = robotManageDao.getHibernateSession().createSQLQuery(
                hql.toString());

        // if (!StringUtils.isEmpty(robotManageQuery.getId())) {
        // query.setParameterList("idList", idList);
        // }

        if (robotManageQuery.getPagerCount() == -1) {
            robotManageQuery.setPagerCount(query.list().size());
        }

        query.setFirstResult(robotManageQuery.getPagerOffset());
        query.setMaxResults(robotManageQuery.getPagerPerPage());

        robotManageQuery.setQueryResults(query.list());
    }

    public List<Object[]> getAllRobotManage(String id) {
        List<RegionManageBean> childList = tbRegionManageDao
                .getChildRegionList(Integer.parseInt(id));
        List<Integer> idList = new ArrayList<Integer>();
        for (RegionManageBean bean : childList) {
            idList.add(bean.getIdRegion());
        }

        //
        idList.add(Integer.parseInt(id));

        StringBuilder hql = new StringBuilder();
        hql
                .append("select t.IDROBOT,t.NAME,r.REGIONNAME,t.CHANNEL from ROBOTMANAGE t where t.IDROBOT in (:idList)");

        Query query = robotManageDao.getHibernateSession().createSQLQuery(
                hql.toString());
        query.setParameterList("idList", idList);

        return query.list();
    }

    public void getRealtimeQosRecord(RobotRealtimeBaan pRaaltimeBean) {
        Query query = robotManageDao
                .getHibernateSession()
                .createQuery(
                        " select t from QosRecord t where t.idRobot=:idRobot order by t.logTime desc ");
        query.setParameter("idRobot", pRaaltimeBean.getRobotManage()
                .getIdRobot());
        query.setMaxResults(1);
        pRaaltimeBean.setQosRecord((QosRecord) query.uniqueResult());

    }

    /**
     * 
     * @param roleSelRobotQuery
     */
    @SuppressWarnings("unchecked")
    public void queryRoleSelRobotList(RoleSelRobotQuery roleSelRobotQuery) {
        List<RoleRobotSelBaan> returnList = new ArrayList<RoleRobotSelBaan>();
        StringBuilder hql = new StringBuilder();
        hql
                .append("select {t.*},{n.*},{r.*},{l.*} from RobotManage t left join RobotNetInfo n on t.IDROBOT=n.IDROBOT ,RoleGroupRobot l,RoleGroup r WHERE l.idRoleGroup =r.ID and l.IdRobot=t.IdRobot ");

        if (null != roleSelRobotQuery.getQueryIdRoleGroup()) {
            hql.append(" and r.id =:idRoleGroup");
        }

        Query query = robotManageDao.getHibernateSession().createSQLQuery(
                hql.toString()).addEntity("t", RobotManage.class).addEntity(
                "n", RobotNetInfo.class).addEntity("r", RoleGroup.class)
                .addEntity("l", RoleGroupRobot.class);

        if (null != roleSelRobotQuery.getQueryIdRoleGroup()) {
            query.setParameter("idRoleGroup", roleSelRobotQuery
                    .getQueryIdRoleGroup());
        }

        query.setFirstResult(roleSelRobotQuery.getPagerOffset());
        query.setMaxResults(roleSelRobotQuery.getPagerPerPage());

        List<Object[]> list = query.list();

        if (roleSelRobotQuery.getPagerCount() == -1) {
            roleSelRobotQuery.setPagerCount(list.size());
        }

        for (Object[] objs : list) {
            RoleRobotSelBaan bean = new RoleRobotSelBaan();
            bean.setRobotManage((null == objs[0]) ? new RobotManage()
                    : (RobotManage) objs[0]);
            bean.setRobotNetInfo((null == objs[1]) ? new RobotNetInfo()
                    : (RobotNetInfo) objs[1]);
            bean.setRoleGroup((null == objs[2]) ? new RoleGroup()
                    : (RoleGroup) objs[2]);
            bean.setRoleGroupRobot((null == objs[3]) ? new RoleGroupRobot()
                    : (RoleGroupRobot) objs[3]);

            returnList.add(bean);

        }

        roleSelRobotQuery.setQueryResults(returnList);

    }

    public void add(RobotManageOBJ robotManageO) {
        robotManageDao.create(convertToRobotManage(robotManageO));
    }

    public void add(RobotCFGOBJ robotCFGO) {
        robotCfgDao.create(convertToRobotCFG(robotCFGO));
    }

    public void add(RobotFileInfoOBJ robotFileInfoO) {
        robotFileInfoDao.create(convertToRobotFileInfo(robotFileInfoO));
    }

    public void add(RobotNetInfoOBJ robotNetInfoO) {
        robotNetInfoDao.create(convertToRobotNetInfo(robotNetInfoO));
    }

    public void delete(int[] ids) {
        for (int id : ids) {
            robotManageDao.deleteRelGroup(id);
            robotManageDao.deleteRelRoleGroup(id);
            robotManageDao.delete(id);
        }
    }

    public void deleteRobotCFG(int[] ids) {
        for (int id : ids) {
            robotCfgDao.delete(id);
        }
    }

    public void deleteRobotFileInfo(int[] ids) {
        for (int id : ids) {
            robotFileInfoDao.delete(id);
        }
    }

    public void deleteRobotNetInfo(int[] ids) {
        for (int id : ids) {
            robotNetInfoDao.delete(id);
        }
    }

    public void deleteRobotGroupManage(int[] pIds) {
        for (int id : pIds) {
            robotManageDao.deleteRelGroup(id);
        }
    }

    public void deleteRelRoleGroup(int[] pIds) {
        for (int id : pIds) {
            robotManageDao.deleteRelRoleGroup(id);
        }
    }

    public void modify(RobotManageOBJ robotManageO) {
        if (robotManageDao.find(robotManageO.getIdRobot()) != null) {
            robotManageDao.update(convertToRobotManage(robotManageO));
        }
    }

    /**
     * @param realtimeBean
     */
    public void getRealtimeQuality(RobotRealtimeBaan realtimeBean) {
        Integer idRobot = realtimeBean.getRobotManage().getIdRobot();
        StringBuilder sql = new StringBuilder();
        sql.append("{call pr_iptv_curquality(?)}");
        Session session = robotManageDao.getHibernateSession();
        Connection conn = session.connection();
        CallableStatement callstmt = null;
        ResultSet rs = null;
        try {
            callstmt = conn.prepareCall(sql.toString());
            callstmt.setInt(1, idRobot);
            rs = callstmt.executeQuery();
            if (rs.next()) {
                realtimeBean.setCurrQuality(rs.getDouble("Quality"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JdbcUtil.close(rs, callstmt, conn);
        }

    }

    /**
     * @param realtimeBean
     */
    public void getRealtimeLostRate(RobotRealtimeBaan realtimeBean) {
        Integer idRobot = realtimeBean.getRobotManage().getIdRobot();
        StringBuilder sql = new StringBuilder();
        sql.append("{call pr_iptv_curlostrate(?)}");
        Session session = robotManageDao.getHibernateSession();
        Connection conn = session.connection();
        CallableStatement callstmt = null;

        ResultSet rs = null;
        try {
            callstmt = conn.prepareCall(sql.toString());
            callstmt.setInt(1, idRobot);
            rs = callstmt.executeQuery();
            if (rs.next()) {
                realtimeBean.setCurrLostRate(rs.getDouble("LostRate"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JdbcUtil.close(rs, callstmt, conn);
        }

    }

    public RobotCFGOBJ getRobotCFGOBJ(int id) {
        RobotCFGOBJ robotCFGO = null;
        RobotCFG robotCFG = robotCfgDao.find(id);
        if (robotCFG != null) {
            robotCFGO = convertToRobotCFGOBJ(robotCFG);
        }
        return robotCFGO;
    }

    public RobotFileInfoOBJ getRobotFileInfoOBJ(int id) {
        RobotFileInfoOBJ robotFileInfoO = null;
        RobotFileInfo robotFileInfo = robotFileInfoDao.find(id);
        if (robotFileInfo != null) {
            robotFileInfoO = convertToRobotFileInfoOBJ(robotFileInfo);
        }
        return robotFileInfoO;
    }

    public RobotNetInfoOBJ getRobotNetInfoOBJ(int id) {
        RobotNetInfoOBJ robotNetInfoO = null;
        RobotNetInfo robotNetInfo = robotNetInfoDao.find(id);
        if (robotNetInfo != null) {
            robotNetInfoO = convertToRobotNetInfoOBJ(robotNetInfo);
        }
        return robotNetInfoO;
    }

    public void modify(RobotCFGOBJ robotCFGO) {
        if (robotCfgDao.find(robotCFGO.getIdRobot()) != null) {
            robotCfgDao.update(convertToRobotCFG(robotCFGO));
        }
    }

    public void modify(RobotFileInfoOBJ robotFileInfoO) {
        if (robotFileInfoDao.find(robotFileInfoO.getIdRobot()) != null) {
            robotFileInfoDao.update(convertToRobotFileInfo(robotFileInfoO));
        }
    }

    public void modify(RobotNetInfoOBJ robotNetInfoO) {
        if (robotNetInfoDao.find(robotNetInfoO.getIdRobot()) != null) {
            robotNetInfoDao.update(convertToRobotNetInfo(robotNetInfoO));
        }
    }

    /**
     * 获取节点状态列表
     * @param userId
     * @param lastIpUpdateTime
     * @return Map
     */
    public Map<Integer, Map<Integer, RobotNodeStatusBean>> getNodeStatusListByUser(
            Integer userId, Date lastIpUpdateTime) {
        Map<Integer, Map<Integer, RobotNodeStatusBean>> nodeStatusMap = robotManageDao
                .getNodeStatusListByUser(userId, lastIpUpdateTime);
        return nodeStatusMap;
    }

    /**
     * 获取节点告警列表
     * @param userId
     * @param startTime
     * @param endTime
     * @return Map
     */
    public Map<Integer, RobotNodeStatusBean> getNodeAlertListByUser(
            Integer userId, Date startTime, Date endTime) {
        return robotManageDao
                .getNodeAlertListByUser(userId, startTime, endTime);
    }

    /**
     * 获取节点告警列表
     * @param pUserId
     * @param pStartTime
     * @param pEndTime
     * @param topn
     * @return List
     */
    public List<RobotNodeStatusBean> getNodeStatusAlertsByUser(Integer pUserId,
            Date pStartTime, Date pEndTime, Integer topn) {
        return robotManageDao.getNodeStatusAlertsByUser(pUserId, pStartTime,
                pEndTime, topn);
    }

    /**
     * 获取节点状态列表
     * @param pUserId
     * @return List
     */
    public List<RobotNodeStatusBean> getNodeStatusListByUser(Integer pUserId) {
        return robotManageDao.getNodeStatusListByUser(pUserId);
    }

    /**
     * 获取最新节点数据
     * @param pUserId
     * @return List
     */
    public List<RobotNodeStatusBean> getLastNodeDataListByUser(
            Integer pUserId){
        Map<Integer, RobotNodeStatusBean> nodeMap= robotManageDao.getNodeStatusMapByUser(pUserId);        
        Calendar calendar =Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY)-2);
        
        Calendar before15min =Calendar.getInstance();
        before15min.set(Calendar.MINUTE, before15min.get(Calendar.MINUTE)-15);
        
        Calendar before24hour =Calendar.getInstance();
        before24hour.set(Calendar.HOUR_OF_DAY, before24hour.get(Calendar.HOUR_OF_DAY)-24);
        
        Calendar endTime =Calendar.getInstance();
        
        List<RobotNodeStatusBean> result_list =new ArrayList<RobotNodeStatusBean>();
        
        for (Integer idRobot:nodeMap.keySet()){
            IptvQuality iptvQuality =robotManageDao.getNodeIptvQualityByIdRobot(idRobot,calendar.getTimeInMillis()/1000);            
            IptvLostRate iptvLostRate = robotManageDao.getNodeIptvLostRateByIdRobot( idRobot,calendar.getTimeInMillis()/1000);
            nodeMap.get(idRobot).setLastIptvLostRate(iptvLostRate);
            nodeMap.get(idRobot).setLastIptvQuality(iptvQuality);             
            Long times_15min=robotManageDao.getNodeAlertCountByIdRobot(idRobot,before15min.getTime(),endTime.getTime());
            Long times_24hour=robotManageDao.getNodeAlertCountByIdRobot(idRobot,before24hour.getTime(),endTime.getTime());
            
            nodeMap.get(idRobot).setTimes15min(times_15min);
            nodeMap.get(idRobot).setTimes24hour(times_24hour);
            
            result_list.add(nodeMap.get(idRobot));
        }
        
        return result_list;        
    }

    /**
     * 
     */
    public Integer queryAllRobotManagers() {
        return robotManageDao.findAll().size();
    }

    /**
     * @param tregion RegionManage(po)
     * @return region RegionManageOBJ(vo)
     */
    public RobotManageOBJ convertToRobotManageOBJ(RobotManage robotManage) {
        RobotManageOBJ robotManageO = new RobotManageOBJ();
        BeanConverter.copyProperties(robotManageO, robotManage);
        return robotManageO;
    }

    /**
     * 
     * @param region
     * @return
     */
    public RobotManage convertToRobotManage(RobotManageOBJ robotManageOBJ) {
        RobotManage robotManage = new RobotManage();
        BeanConverter.copyProperties(robotManage, robotManageOBJ);
        RegionManageOBJ obj = robotManageOBJ.getRegionManageOBJ();
        if (null != obj) {
            RegionManage region = new RegionManage();
            region.setIdRegion(obj.getIdRegion());
            region.setPrentRegionId(obj.getPrentRegionId());
            region.setRegionName(obj.getRegionName());

            robotManage.setRegionManage(region);
        }

        return robotManage;
    }

    public RobotCFGOBJ convertToRobotCFGOBJ(RobotCFG robotCFG) {
        RobotCFGOBJ robotCFGO = new RobotCFGOBJ();
        BeanConverter.copyProperties(robotCFGO, robotCFG);
        return robotCFGO;
    }

    /**
     * @param region
     * @return
     */
    public RobotCFG convertToRobotCFG(RobotCFGOBJ robotCFGO) {
        RobotCFG robotCFG = new RobotCFG();
        BeanConverter.copyProperties(robotCFG, robotCFGO);
        return robotCFG;
    }

    public RobotFileInfoOBJ convertToRobotFileInfoOBJ(
            RobotFileInfo robotFileInfo) {
        RobotFileInfoOBJ robotFileInfoO = new RobotFileInfoOBJ();
        BeanConverter.copyProperties(robotFileInfoO, robotFileInfo);
        return robotFileInfoO;
    }

    /**
     * @param region
     * @return
     */
    public RobotFileInfo convertToRobotFileInfo(RobotFileInfoOBJ robotFileInfoO) {
        RobotFileInfo robotFileInfo = new RobotFileInfo();
        BeanConverter.copyProperties(robotFileInfo, robotFileInfoO);
        return robotFileInfo;
    }

    public RobotNetInfoOBJ convertToRobotNetInfoOBJ(RobotNetInfo robotNetInfo) {
        RobotNetInfoOBJ robotNetInfoO = new RobotNetInfoOBJ();
        BeanConverter.copyProperties(robotNetInfoO, robotNetInfo);
        return robotNetInfoO;
    }

    /**
     * @param region
     * @return
     */
    public RobotNetInfo convertToRobotNetInfo(RobotNetInfoOBJ robotNetInfoO) {
        RobotNetInfo robotNetInfo = new RobotNetInfo();
        BeanConverter.copyProperties(robotNetInfo, robotNetInfoO);
        return robotNetInfo;
    }

    public void setRobotManageDao(RobotManageDao pRobotManageDao) {
        robotManageDao = pRobotManageDao;
    }

    public void setRobotCfgDao(RobotCfgDao pRobotCfgDao) {
        robotCfgDao = pRobotCfgDao;
    }

    public void setRobotFileInfoDao(RobotFileInfoDao pRobotFileInfoDao) {
        robotFileInfoDao = pRobotFileInfoDao;
    }

    public void setRobotNetInfoDao(RobotNetInfoDao pRobotNetInfoDao) {
        robotNetInfoDao = pRobotNetInfoDao;
    }

    public void setTbRegionManageDao(TbRegionManageDao pTbRegionManageDao) {
        tbRegionManageDao = pTbRegionManageDao;
    }

}
